#include "tzfe.h"
#include "ui_tzfe.h"
#include<QSoundEffect>

tzfe::tzfe(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::tzfe)
{

    ui->setupUi(this);
    //初始化
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            s[i][j]=0;
        }
    }


    button=new QPushButton("开始游戏",this);
    button->setGeometry(200,700,218,100);//放置按钮在130,830处放置一个宽高为358,100的按钮
    button->setStyleSheet("background-color:rgb(242,156,177);font:25pt");
    //随机种子
    qsrand(uint(QTime(0,0,0).secsTo(QTime::currentTime())));
    //槽与信号
    connect(button,SIGNAL(clicked()),this,SLOT(slotStart()));
    setFocusPolicy(Qt::StrongFocus);
}

tzfe::~tzfe()
{
    delete ui;
}

void tzfe::paintEvent(QPaintEvent *)
{
      QPainter painter(this);

       painter.setBrush(Qt::white);
       painter.setFont(QFont("宋体",35,1000,false));
 painter.drawPixmap(0,0,618,850,QPixmap(":/background/th.jpg"));
       QString strscore;
       painter.drawText(QPoint(80,100),"你的分数:"+QString::number(score));
       //实现格子
       for (int i=0;i<4;i++) {
              for (int j=0;j<4;j++) {
                  painter.setPen(Qt::transparent);//?
                  if(s[i][j]==0){
                    painter.setBrush(Qt::white);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                }
                else if(s[i][j]==2)
                {
                    painter.setBrush(Qt::red);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                    painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(2),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==4)
                {
                    painter.setBrush(Qt::cyan);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(4),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==8)
                {
                    painter.setBrush(Qt::green);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(8),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==16)
                {
                    painter.setBrush(Qt::magenta);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(16),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==32)
                {
                    painter.setBrush(Qt::yellow);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(32),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==64)
                {
                    painter.setBrush(Qt::darkYellow);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(64),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==128)
                {
                    painter.setBrush(Qt::darkRed);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(128),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==256)
                {
                    painter.setBrush(Qt::darkCyan);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(256),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==512)
                {
                    painter.setBrush(Qt::darkGreen);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(512),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==1024)
                {
                    painter.setBrush(Qt::darkMagenta);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(1024),QTextOption(Qt::AlignCenter));
                }
                else if(s[i][j]==2048)
                {
                    painter.setBrush(Qt::blue);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(2048),QTextOption(Qt::AlignCenter));
                }
                else
                {
                    painter.setBrush(Qt::darkBlue);
                    painter.drawRect(i*119+80,j*119+182,101,101);
                     painter.setPen(Qt::black);
                     painter.setFont(QFont("宋体",25,500,false));
                     painter.drawText(QRectF(i*119+80,j*119+182,101,101),QString::number(s[i][j]),QTextOption(Qt::AlignCenter));
                }
           }
       }
}

void tzfe::keyPressEvent(QKeyEvent *event)
{
    if(!state){
           return ;
       }
           switch (event->key()) {
           case Qt::Key_W:
           case Qt::Key_Up:
               Pressup();
               break;

           case Qt::Key_S:
           case Qt::Key_Down:
               Pressdown();
               break;

           case Qt::Key_A:
           case Qt::Key_Left:
               Presslift();
               break;

           case Qt::Key_D:
           case Qt::Key_Right:
               Pressright();
               break;

           default:
               return;//忽略其他按钮
           }
           myRand();
           update();
}

void tzfe::Pressup()
{ //移动
    for (int i=0;i<4;i++) {
        for (int j=1;j<4;j++) {
            if(s[i][j]==0)continue;
            for (int p=0;p<j;p++) {
                //查看前面是否有空格子可移动
                if(s[i][p]==0){
                    s[i][p]=s[i][j];
                    s[i][j]=0;
                    break;
                }
            }
        }
    }
    //相加
    for (int i=0;i<4;i++) {
        for (int j=0;j<3;j++) {
            if(s[i][j]==s[i][j+1]){
                s[i][j]=2*s[i][j];
                s[i][j+1]=0;
                score+=s[i][j];
                for (int p=j+2;p<4;p++) {
                    s[i][p-1]=s[i][p];
                }
            }
        }
    }
}

void tzfe::Pressdown()
{
    //移动
       for (int i=0;i<4;i++) {
           for (int j=2;j>=0;j--) {
               if(s[i][j]==0)continue;
               for (int p=3;p>j;p--) {
                   //查看前面是否有空格子可移动
                   if(s[i][p]==0){
                       s[i][p]=s[i][j];
                       s[i][j]=0;
                       break;
                   }
               }
           }
       }
       //相加
       for (int i=0;i<4;i++) {
           for (int j=3;j>0;j--) {
               if(s[i][j]==s[i][j-1]){
                   s[i][j]=2*s[i][j];
                   s[i][j-1]=0;
                   score+=s[i][j];
                   for (int p=j-2;p>=0;p--) {
                       s[i][p+1]=s[i][p];
                   }
               }
           }
       }
}

void tzfe::Presslift()
{
    //移动
       for (int j=0;j<4;j++) {
           for (int i=1;i<4;i++) {
               if(s[i][j]==0){
                   continue;
               }
               for (int p=0;p<i;p++) {
                   //查看前面是否有空格可移入
                   if(s[p][j] == 0){
                       s[p][j] = s[i][j];
                       s[i][j] = 0;
                       break;
                   }
               }
           }
       }
       //相加
       for (int j=0;j<4;j++) {
           for (int i=0;i<3;i++) {
               if(s[i][j]==s[i+1][j]){
                   s[i][j]=s[i][j]*2;
                   score+=s[i][j];
                   s[i+1][j]=0;
                   for(int p=i+2;p<4;p++){
                       s[p-1][j] = s[p][j];
                  }
               }
           }
       }
}

void tzfe::Pressright()
{
    //移动
        for (int j=0;j<4;j++) {
            for (int i=2;i>=0;i--) {
                if(s[i][j]==0){
                    continue;
                }
                for (int p=3;p>i;p--) {
                    //查看前面是否有空格可移入
                    if(s[p][j] == 0){
                        s[p][j] = s[i][j];
                        s[i][j] = 0;
                        break;
                    }
                }
            }
        }
        //相加
        for (int j=0;j<4;j++) {
            for (int i=3;i>=0;i--) {
                if(s[i][j]==s[i-1][j]){
                    s[i][j]=s[i][j]*2;
                    s[i-1][j]=0;
                    score+=s[i][j];
                    for(int p=i-2;p>=0;p--){
                        s[p+1][j] = s[p][j];
                    }
                }
            }
        }
}

void tzfe::myRand()
{
    int i=0,j=0;
    struct N n[15];
    int ni=0;
        for (i=0;i<4;i++) {
            for (j=0;j<4;j++) {
                if(s[i][j]==0){
                    n[ni].i=i;
                    n[ni].j=j;
                    ni++;
                }
            }
        }

        //判断游戏是否结束
        if (ni==0) {
            for (i=0;i<4;i++) {
                for (j=0;j<3;j++) {
                    if(s[i][j]==s[i][j+1]){
                        return;
                    }
                }
            }
            for (j=0;j<4;j++) {
                for (i=0;i<3;i++) {
                    if(s[i][j]==s[i+1][j]){
                        return;
                    }
                }
            }
            QMessageBox::about(this,"游戏失败","你的分数为："+QString::number(score)+QString("<br>别灰心 再来一次吧"));
            return;
        }
        int rand=qrand()%ni;
        s[n[rand].i][n[rand].j]=2;
}



void tzfe::slotStart()
{
    QMessageBox::about(this,"2048游戏规则","使用WASD或↑↓←→控制所有方块上下左右移动");
    score=0;
    for (int i=0;i<4;i++) {
          for (int j=0;j<4;j++) {
              s[i][j]=0;
          }
      }
      button->setText("重新游戏");

      int randi=qrand()%4;
      int randj=qrand()%4;
      s[randi][randj]=2;

      state=true;
      update();
}

